package acn.spring.core.interceptor;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import acn.spring.domain.Product;

@Aspect
public class MyAspect {

	@Before("execution(* acn.spring.service.ShopService.addProduct(..))")
	public void addProductValidation(JoinPoint joinPoint) {
		System.out.println("called Method:"
				+ joinPoint.getSignature().getName());
		Product Product = (Product) joinPoint.getArgs()[0];
		System.out.println("added Product Name:" + Product.getName());
		System.out.println("added Product Id:" + Product.getId());
	}

	@AfterReturning(pointcut = "execution(* acn.spring.service.ShopService.addProduct(..))", returning = "Product")
	public void addProductPostExecution(JoinPoint joinPoint, Object Product) {
		System.out.println("persisted Object Name:"
				+ ((Product) Product).getName());
		System.out
				.println("persisted Object Id:" + ((Product) Product).getId());
	}

	// @Around("execution(* acn.spring.service.ProductService.addProduct(..))")
	// public void around(ProceedingJoinPoint joinPoint) throws Throwable {
	// System.out.println("AROUND before Method:" +
	// joinPoint.getSignature().getName());
	// joinPoint.proceed();
	// System.out.println("AROUND after Method:" +
	// joinPoint.getSignature().getName());
	// }
}
